package top.huntwolf.admin.component;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import top.huntwolf.admin.annotation.URLControl;
import top.huntwolf.admin.annotation.URLMessage;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("urlMessageCenter")
public class URLMessageCenter {
    private static List<Map<String,String>> urls;

    @Autowired
    ApplicationContext applicationContext;

    public void loadURL(){
        urls = new ArrayList<>();
        Map o = applicationContext.getBeansWithAnnotation(URLControl.class);
        for(Object key:o.keySet()){
            Method[] methods = o.get(key).getClass().getMethods();
            for(int i = 0;i < methods.length;i++){
                URLMessage message = methods[i].getAnnotation(URLMessage.class);
                if(message != null){
                    Map url = new HashMap();
                    url.put("url",message.url());
                    urls.add(url);
                }
            }
        }
    }
    public List<Map<String,String>> getURL(){
        if(urls == null){
            synchronized (URLMessageCenter.class){
                if(urls == null){
                    loadURL();
                }
            }
        }
        return urls;
    }
}
